<?php
namespace app\common\model;
use \think\Db;
class Log{
    
    /**
     * 添加管理员操作日志
     * @param string $msg
     * @param unknown $user_id 被操作会员
     */
    static public function admin( $msg='', $user_id=0, $table="user" ){
        $admin_user_id = com_userinfo('id');
        $admin_username= com_userinfo('username');
        if( !$admin_user_id )
            return com_error(['请登录后再操作!']);
        
        $msg  = $admin_username."于".date("Y-m-d H:i")."-".$msg;
        if($user_id){
            $msg .= "-For".Db::name($table)->where('id', $user_id)->value('name')."[{$user_id}]";
        }
        $data = [
            'action_user_id'    =>  $admin_user_id,
            'user_id'           =>  $user_id,
            'msg'               =>  $msg,
            'type'              =>  0,
            'm'                 =>   com_module(),
            'c'                 =>   com_controller(),
            'a'                 =>   com_action(),
            'ip'                => com_client_ip(),
            'create_time'       =>   time(),
        ];
        return Db::name('log')->insertGetId($data);
    }
    /**
     * 会员日志
     * @param string $msg
     * @param number $user_id  被操作会员
     * @return \my\false;
     */
    static public function user($msg='', $user_id=0, $table="user" ){
        $action_user_id   = com_userinfo('id');
        $action_user_name = com_userinfo('name');
    
        if( !$action_user_id )
            return com_error(['请登录后再操作!']);
    
        $msg  = $action_user_name."于".date("Y-m-d H:i")."-".$msg;
        if($action_user_id){
            $msg .= "-For".Db::name($table)->where('id', $user_id)->value('name')."[{$user_id}]";
        }
        $data = [
            'action_user_id'    =>  $action_user_id,
            'user_id'           =>   $user_id,
            'type'              => 1,
            'msg'               =>   $msg,
            'm'                 =>   com_module(),
            'c'                 =>   com_controller(),
            'a'                 =>   com_action(),
            'ip'                => com_client_ip(),
            'create_time'       =>   time(),
        ];
        return Db::name('log')->insertGetId($data);
    }
    
    /**
     * 系统日志
     * @param unknown $msg
     */
    static public function sys($msg){
        $data = [
            'type'    => 0,
            'msg'     =>   $msg,
            'm'       =>   com_module(),
            'c'       =>   com_controller(),
            'a'       =>   com_action(),
            'ip'                => com_client_ip(),
            'create_time'       =>   time(),
        ];
        return Db::name('log')->insertGetId($data);
    }
}